package com.example.control;

import com.example.entry.Books;
import com.example.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller

public class MainController {

    @Resource
    BookService bookService;

    @RequestMapping("/test")
    public void test(){
        List<String> booksList=bookService.booktest();
        System.out.println(booksList);
    }



//    首页
    @RequestMapping("/index")
    public String index() {
        return "/index";
    }

    //    测试播放器页面
    @RequestMapping("/a")
    public String index5() {
        return "speaker";
    }


//    这里是显示所有书籍和模糊查询控制
    @RequestMapping(value = "allBook")
    public String getBooksList(@RequestParam(value = "bookName",required = false)String bookName,Model model) {
        System.out.println(bookName);//测试接收到的bookName数据
        List<Books> booksList;
//        判空操作，如果判断没有输入信息，就模糊查询所有数据
        if (bookName==null){
            booksList = bookService.bookList("%");
        }else {
//        如果有输入的数据，那么将使用正规的模糊查询
            booksList = bookService.bookList("%"+bookName+"%");
        }
        model.addAttribute("books",booksList);
        return "/allBook";
    }



    //        下面的情况被忽略，只作为备份数据
//        注意：下面这些数据有bug，如果你什么数据也不输入，他不会回显数据
//        if (bookName==null){
//            List<Books> booksList = bookService.bookList("%");
//            System.out.println(booksList);
//            model.addAttribute("books", booksList);
//        }else{
//            List<Books> booksList = bookService.bookList(bookName);
//            model.addAttribute("books",booksList);
//        }





    //    这里是添加过后的逻辑
    @RequestMapping(value = "addED",method = RequestMethod.POST)
    public String addBook(@RequestParam("bookName")String bookName,
                          @RequestParam("bookCounts")int bookCounts,
                          @RequestParam("detail")String detail){
        bookService.addBook(bookName,bookCounts,detail);
        return "redirect:/allBook";
    }


//    这里是添加之前的逻辑
    @RequestMapping("/adding")
    public String index4() {
        return "/addBook";
    }


    //    这里是删除逻辑，删除完了直接重定向
    @RequestMapping(value = "delBook",method = RequestMethod.GET)
    public String addBook(@RequestParam("bookID") int bookID){
        bookService.Del(bookID);
        return "redirect:allBook";
    }


//    修改前
    @RequestMapping("/update")
    public String update(@RequestParam("bookID") int bookID,Model model){
        Books book =bookService.findByID(bookID);
        model.addAttribute("book",book);
        return "updateBook";
    }
//    修改后重定向
    @RequestMapping("/updateBook2")
    public String updateBook2(@ModelAttribute(value = "demo") Books demo, Model model){
//        这里是一个测试，测试提交完信息的内容是否会改变
        System.out.println(demo.toString()+"这里是接收到的信息");
        bookService.update(demo.getBookID(),demo.getBookName(),demo.getBookCounts(),demo.getDetail());
        return "redirect:allBook";
    }







}
